//(function($){ ..jquery代码..   })(jQuery); 该格式里面的jquery代码会马上执行
(function($){
  window.Message = {
    SUCCESS:"success",
    INFO:"info",
    WARNING:"warning",
    DANGER:"danger",
    success:function(msg){
      this.alert(msg,this.SUCCESS);
    },
    info:function(msg){
      this.alert(msg,this.INFO);
    },
    warning:function(msg){
      this.alert(msg,this.WARNING);
    },
    danger:function(msg){
      this.alert(msg,this.DANGER);
    },
    alert:function(msg,type){
      // 未传入type则默认为success类型的消息框
      if(typeof(type) =="undefined") {
        type = "success";
      }
      // 创建bootstrap的alert元素
      var div = $("<div></div>")
                  .addClass('alert')
                  .addClass('alert-'+type)
                  .addClass('alert-dismissible')
                  .addClass("fade show");
      // 消息框的定位样式：顶部居中
      div.css({
        "position": "absolute",
        "top": "80px",
        "left": "50%",
        "width": "400px",
        "margin-left": "-200px"
      });
      // 设置消息框的内容
      div.text(msg);
      // 消息框添加可以关闭按钮
      var closeBtn = $('<button type="button" class="btn-close" data-bs-dismiss="alert"></button>');
      $(div).append(closeBtn);
      // 消息框放入到页面中
      $('body',window.top.document).append(div);
      return div;
    }
  };

  window.Dialog = {
    detail: function(opitions){
      var windowWidth = $(window).width()-10;
      var windowHeight = $(window).height()-10;

      var detailDiv = $('#detailModal');
      if(detailDiv&&detailDiv.length>0){
        $('#detailModal .modal-title').html(opitions.title);
        $('#detailModal iframe').attr("src",opitions.url);
      }else{
        var iframe = $('<iframe class="w-100 h-100"></iframe>');
        iframe.attr("src",opitions.url);
        var body = $('<div class="modal-body"/>');
        body.append(iframe);

        var title = $('<h5 class="modal-title"/>');
        title.append(opitions.title);
        var close = $('<button type="button" class="btn-close" data-bs-dismiss="modal"></button>');
        var header = $('<div class="modal-header"/>');
        header.append(title);
        header.append(close);

        var yes = $('<button type="button" class="btn btn-primary" id="saveDetailBtn">保存</button>');
        var no = $('<button type="button" class="btn btn-secondary" id="cancelDetailBtn">取消</button>');
        var footer = $('<div class="modal-footer"/>');
        footer.append(yes);
        footer.append(no);

        var content = $('<div class="modal-content"/>');
        content.css("width",windowWidth+"px");
        content.css("height",windowHeight+"px");
        content.append(header);
        content.append(body);
        content.append(footer);

        var dialog = $('<div class="modal-dialog"/>');
        dialog.css("margin","0");
        dialog.css("max-width",windowWidth+"px");
        dialog.css("max-height",windowHeight+"px");
        dialog.append(content);

        detailDiv = $('<div class="modal fade modal-dialog position-absolute" id="detailModal"/>');
        detailDiv.css("margin","5px");
        detailDiv.css("max-width",windowWidth+"px");
        detailDiv.css("max-height",windowHeight+"px");
        detailDiv.append(dialog);

        // 消息框放入到页面中
        $('body').append(detailDiv);
      }

      $('#detailModal').modal('show');
      $("#saveDetailBtn").unbind('click').click(function () {
        if(opitions.callback){
          var detail = $('#detailModal iframe')[0].contentWindow;
          if(opitions.getDataMethod){
            var data = $(detail)[0][opitions.getDataMethod]();
            opitions.callback(true,data);
          }else{
            opitions.callback(true,detail);
          }
        }
        $('#detailModal').modal('hide');
      });
      $("#cancelDetailBtn").unbind('click').click(function () {
        if(opitions.callback){
          opitions.callback(false);
        }
        $('#detailModal').modal('hide');
      });
    },
    confirm: function (opitions) {
      var confirmDiv = $('#confirmModal');
      if(confirmDiv&&confirmDiv.length>0){
        $('#confirmModal .modal-title').html(opitions.title);
        $('#confirmModal .modal-body').html('<p>'+opitions.message+'</p>');
      }else{
        confirmDiv = $('<div class="modal fade modal-dialog position-absolute" id="confirmModal" style="margin-top:100px;margin-left: -200px;left:50%"/>');
        var dialog = $('<div class="modal-dialog"/>');
        var content = $('<div class="modal-content"/>');
        var header = $('<div class="modal-header"/>');
        var body = $('<div class="modal-body" style="height: 100px;width: 400px"/>');
        var footer = $('<div class="modal-footer"/>');
        var title = $('<h5 class="modal-title"/>');
        var close = $('<button type="button" class="btn-close" data-bs-dismiss="modal"></button>');
        var yes = $('<button type="button" class="btn btn-primary" id="confirmBtn">确认</button>');
        var no = $('<button type="button" class="btn btn-secondary" id="cancelBtn">取消</button>');
        title.append(opitions.title);
        header.append(title);
        header.append(close);
        content.append(header);
        body.append($('<p>'+opitions.message+'</p>'));
        content.append(body);
        footer.append(yes);
        footer.append(no);
        content.append(footer);
        dialog.append(content);
        confirmDiv.append(dialog);
        // 消息框放入到页面中
        $('body').append(confirmDiv);
      }
      $('#confirmModal').modal('show');
      $("#confirmBtn").unbind('click').click(function () {
        if(opitions.callback){
          opitions.callback(true);
        }
        $('#confirmModal').modal('hide');
      });
      $("#cancelBtn").unbind('click').click(function () {
        if(opitions.callback){
          opitions.callback(false);
        }
        $('#confirmModal').modal('hide');
      });
    }
  };

  window.getQueryVariable = function(variable){
    var value = null;
    var query = window.location.search;
    if(query&&query.indexOf("?")>-1){
      query = query.substring(1);
      var items = query.split("&");
      items.forEach(function (item) {
        var pair = item.split("=");
        if(pair[0]==variable){
          value = decodeURI(pair[1]);
          return;
        }
      });
    }
    return value;
  };

  $.ajaxSetup({
    beforeSend: function (beforeXhr,options) {
      beforeXhr.setRequestHeader("refreshToken","true");
      var originalSuccess = options.success
      options.success = function(data, textStatus, successXhr) {
        if(data&&data.code==403){
          window.top.location.href="/springmvc-demo/login.htmlx";
        }else if(data&&data.code==503){
          Message.danger(data.msg);
        }else{
          var token = data.token;
          if(!token){
            token = successXhr.getResponseHeader("token");
          }
          if(token){
            $.cookie('token', token,{path:"/"});
          }
          originalSuccess.apply(this, arguments);
        }
      }
    }
  });
})(jQuery);